package com.zz.parkingsystem.util;

import com.zz.parkingsystem.entity.Strategy;
import com.zz.parkingsystem.entity.Weekday;
import com.zz.parkingsystem.entity.Weekend;
import com.zz.parkingsystem.mapper.WeekdayMapper;
import com.zz.parkingsystem.mapper.WeekendMapper;


import java.math.BigDecimal;
import java.util.Date;
public class ComputeUtil {

    //通过开始时间和结束时间计算停车时长
    public  String computeTimeByStartAndEnd(Date begin,Date end){
        long start = begin.getTime();
        long endTime = end.getTime();
        int time = (int)(endTime-start)/1000;
        int hours = time/3600;
        int minute = (time - hours*3600)/60;
        int seconds = (time - hours*3600 - minute*60)/60;
        return hours+"小时"+minute+"分钟"+seconds+"秒";
    }
    //通过开始时间，结束时间，停车主策略，计算金额
    public  BigDecimal computeMoney(Date begin, Date end, Strategy strategy,WeekdayMapper weekdayMapper,WeekendMapper weekendMapper){
        int time = (int)(end.getTime()- begin.getTime())/1000;
        int minute = time/60;
        if(minute<=30 && strategy.getIsFree()==1){
            return new BigDecimal(0);
        }
        //周内
        if(!DateUtil.isWeekend(begin)){
            //周末策略
            Weekday weekday = weekdayMapper.selectByPrimaryKey(strategy.getWdId().longValue());
            //繁忙时间
            if(begin.getHours()>weekday.getBusyTimeStart().getHours()){
                return new BigDecimal(minute/60.0*(weekday.getBusyStandardAmount()).doubleValue());
            }
            return new BigDecimal(minute/60.0*(weekday.getNotBusyStandardAmount()).doubleValue());
        }
        //周末
        //周末策略
        Weekend weekend = weekendMapper.selectByPrimaryKey(strategy.getWedId().longValue());
        //繁忙时间
        if(begin.getHours()>weekend.getBusyTimeStart().getHours()){
            return new BigDecimal(minute/60.0*(weekend.getBusyStandardAmount()).doubleValue());
        }
        return new BigDecimal(minute/60.0*(weekend.getNotBusyStandardAmount()).doubleValue());
    }
}
